package com.dgut.hisystem.service.impl;

import com.dgut.hisystem.entity.*;
import com.dgut.hisystem.entity.VO.BalanceVO;
import com.dgut.hisystem.entity.VO.BillVO;
import com.dgut.hisystem.entity.VO.ChargeVO;
import com.dgut.hisystem.entity.VO.DailyVO;
import com.dgut.hisystem.mapper.*;
import com.dgut.hisystem.service.ChargeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChargeServiceImpl implements ChargeService {
    @Autowired
    private ChargeMapper chargeMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Autowired
    private AdviceMapper adviceMapper;
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private CaseMapper caseMapper;

    @Override
    public void insert(Charge charge){
        chargeMapper.insert(charge);
    }

    @Override
    public void updateCharge(int sf_id){
        chargeMapper.updateCharge(sf_id);
    }

    @Override
    public void pay(int sf_id){
        Charge charge = chargeMapper.getChargeById(sf_id);
        Patient patient = patientMapper.selectID(charge.getBr_card());
        double money = patient.getBr_balance() - charge.getSf_cost();
        patientMapper.updateBalance(money,patient);
    }

    @Override
    public void back(int sf_id){
        Charge charge = chargeMapper.getChargeById(sf_id);
        Patient patient = patientMapper.selectID(charge.getBr_card());
        double money = patient.getBr_balance() + charge.getSf_cost();
        patientMapper.updateBalance(money,patient);
        Prescription prescription = prescriptionMapper.getPrescriptionByBl_id(charge.getBl_id());
        adviceMapper.deleteByCf_id(prescription.getCf_id());
        prescriptionMapper.delete(prescription.getCf_id());
    }

    @Override
    public void updateRefund(int sf_id){
        chargeMapper.updateRefund(sf_id);
    }

    @Override
    public Charge getChargeById(int sf_id){
        return chargeMapper.getChargeById(sf_id);
    }

    @Override
    public List<ChargeVO> selectCharge(){
        List<Charge> charges = chargeMapper.selectCharge();
        return getChargeVOS(charges);
    }

    @Override
    public List<ChargeVO> selectRefund(){
        List<Charge> charges = chargeMapper.selectRefund();
        return getChargeVOS(charges);
    }

    @Override
    public List<DailyVO> selectDaily(Timestamp c, Timestamp d){
        List<DailyVO> dailyVOS = new ArrayList<>();
        List<Charge> charges = chargeMapper.selectDaily(c,d);
        return getDailyVOS(dailyVOS, charges);
    }

    @Override
    public List<ChargeVO> selectChargeByCard(long br_card){
        List<Charge> charges = chargeMapper.selectChargeByCard(br_card);
        return getChargeVOS(charges);
    }

    @Override
    public List<ChargeVO> selectRefundByCard(long br_card){
        List<Charge> charges = chargeMapper.selectRefundByCard(br_card);
        return getChargeVOS(charges);
    }

    private List<ChargeVO> getChargeVOS(List<Charge> charges) {
        List<ChargeVO> chargeVOS = new ArrayList<>();
        for (Charge charge : charges) {
            ChargeVO chargeVO = new ChargeVO();
            BeanUtils.copyProperties(charge,chargeVO);
            Patient patient = patientMapper.selectID(charge.getBr_card());
            BeanUtils.copyProperties(patient,chargeVO);
            User user = userMapper.getUserById(patient.getYh_id());
            BeanUtils.copyProperties(user,chargeVO);
            chargeVOS.add(chargeVO);
        }
        return chargeVOS;
    }

    @Override
    public BalanceVO getBalance(long br_card){
        Patient patient = patientMapper.selectID(br_card);
        BalanceVO balanceVO = new BalanceVO();
        BeanUtils.copyProperties(patient,balanceVO);
        if(patient.getBr_sex()==0){
            balanceVO.setBr_sex("男");
        }else {
            balanceVO.setBr_sex("女");
        }
        User user = userMapper.getUserById(patient.getYh_id());
        BeanUtils.copyProperties(user,balanceVO);
        return balanceVO;
    }

    @Override
    public List<BillVO> getBill(int sf_id){
        List<BillVO> billVOS = new ArrayList<>();
        Charge charge = chargeMapper.getChargeById(sf_id);
        Prescription prescription = prescriptionMapper.getPrescriptionByBl_id(charge.getBl_id());
        List<Advice> adviceList = adviceMapper.getAdviceByPrescription(prescription.getCf_id());
        for (Advice advice : adviceList) {
            BillVO billVO = new BillVO();
            BeanUtils.copyProperties(advice,billVO);
            Drug drug = drugMapper.getDrugById((int) advice.getYp_id());
            BeanUtils.copyProperties(drug,billVO);
            billVOS.add(billVO);
        }
        return billVOS;
    }

    @Override
    public List<DailyVO> selectDailyByCard(long br_card,Timestamp c, Timestamp d){
        List<DailyVO> dailyVOS = new ArrayList<>();
        List<Charge> charges = chargeMapper.selectDailyByCard(br_card,c,d);
        return getDailyVOS(dailyVOS, charges);
    }

    private List<DailyVO> getDailyVOS(List<DailyVO> dailyVOS, List<Charge> charges) {
        for (Charge charge : charges) {
            DailyVO dailyVO = new DailyVO();
            BeanUtils.copyProperties(charge,dailyVO);
            Case aCase = caseMapper.selectID(charge.getBl_id());
            BeanUtils.copyProperties(aCase,dailyVO);
            Patient patient = patientMapper.selectID(charge.getBr_card());
            BeanUtils.copyProperties(patient,dailyVO);
            User user = userMapper.getUserById(patient.getYh_id());
            BeanUtils.copyProperties(user,dailyVO);
            if(dailyVO.getSf_charge()==1){
                dailyVO.setCharge(dailyVO.getSf_cost());
                dailyVO.setRefund(0);
            }
            else {
                dailyVO.setCharge(0);
                dailyVO.setRefund(dailyVO.getSf_cost());
            }
            dailyVOS.add(dailyVO);
        }
        return dailyVOS;
    }
}
